#!/bin/bash
# $Id: echo_inbound,v 1.13 2004/10/09 01:19:13 repeater Exp $
# $Log: echo_inbound,v $
# Revision 1.13  2004/10/09 01:19:13  repeater
# Removed an extra unkey statement.
#
# Revision 1.12  2004/09/15 21:40:16  repeater
# Simplify IRLP busy logic.
#
# Revision 1.11  2004/09/12 19:28:21  repeater
# Convert to use parsehosts in echo_common.
#
# Revision 1.10  2004/09/08 21:37:33  repeater
# Removed the line... echo "Could not find $1 in $ECHOHOSTS"
# Just needed the log statement.
#
# Revision 1.9  2004/09/02 02:01:33  repeater
# Stop using echo_on.  Add fork to $SCRIPT/statupdate.
#
# Revision 1.8  2004/08/29 15:58:52  repeater
# Replaced echostatus search with log statement when unable to find node in hosts file.
#
# Revision 1.7  2004/08/29 15:54:09  repeater
# Modify to use echo-hosts file if exists.
#
# Revision 1.6  2004/08/19 05:32:11  repeater
# Added echo_on to connect audio.
#
# Revision 1.5  2004/08/03 20:19:42  repeater
# Corrected echo_wavgen $IRLPSTN to echo_wavgen "$LOCAL"/active.
#
# Revision 1.4  2004/08/03 20:16:50  repeater
# Escape special characters in search argument for any ACL file searches so conference name matches will work.
#
# Revision 1.3  2004/08/03 20:12:46  repeater
# Improve handling of any timeoutvalue left unrestored from previous run.
#
# Revision 1.2  2004/08/03 20:09:45  repeater
# Changed message for "EchoIRLP node disabled" to "This node is disabled".
#
# Revision 1.1  2004/08/03 19:34:43  repeater
# Initial revision
#

# Syntax echo_inbound CALLSIGN No_of_connected_nodes

[ -z "$ECHO_SCRIPT" ] && ECHO_SCRIPT=/home/EchoIRLP/scripts
   source $ECHO_SCRIPT/echo_common

# Check to see if system is configured as a conference
if [ -f "$LOCAL"/echo_conference ]; then
  # tbd is configured as a conference server, so abort to keep it independent.
  exit 0
fi
# Determine if tbd is running else exit
pid=`/sbin/pidof -o $$ -o $PPID -o %PPID -x tbd`
if [ "$pid" = "" ] ; then
  log "tbd is not running..."
  $SCRIPT/wavplay error contactirlp 9 9
  exit 1
fi

# Determine if node is active 
if [ ! -f "$LOCAL"/enable ] || [ ! -f "$LOCAL"/echo_enable ] ; then
   # Node is not enabled, dump any incoming calls.
   TBD_COMMAND "message This node is disabled.  Please try again later."
   sleep 1
   TBD_COMMAND "play4 -u $1 nodedisabled"
   sleep 6
   TBD_COMMAND disconnect $1
   exit 0
fi

# Determines node type for access control
# check for conference first

eval `echo "$1" | awk '/*/ {print "TYPE=conference"}' -`
# Is this a link?
if [ "$TYPE" = "" ] ; then 
  eval `echo "$1" | awk '/-L/ {print "TYPE=link"}' -`
fi
# Is this a repeater?
if [ "$TYPE" = "" ] ; then  
  eval `echo "$1" | awk '/-R/ {print "TYPE=repeater"}' -`
fi
if [ "$TYPE" = "" ] ; then  
  # Must be a Node number or Callsign
  TYPE=user
fi

# Part of the prompt used in text mode
CURS=">"

LOCKOUT=NO

# Check if we're locking out any of the node types
# i.e. conferences, links, repeaters or PC users
if [ -f "$ECHO_CUSTOM"/type_deny ]; then
  if [ `grep -x "$TYPE" "$ECHO_CUSTOM/type_deny"` ]; then
    LOCKOUT=YES
    if [ "$TYPE" = "user" ] ; then
      DENY_MSG="Sorry, This node only accepts connections from RF (-L or -R) nodes."
    else
      DENY_MSG="Sorry, You are not allowed to connect to this node."
    fi
  fi
fi

# Check for nodes which are explicitly locked out
ESCSTR=`echo "$1" | sed 's/\*/\\\*/g'`
if [ -f "$ECHO_CUSTOM"/node_deny ]; then
  if [ `grep -x "$ESCSTR" "$ECHO_CUSTOM/node_deny"` ]; then
    LOCKOUT=YES
    DENY_MSG="Sorry, You are not allowed to connect to this node."
  fi
fi

# Check for nodes explicitly allowed.
if [ -f "$ECHO_CUSTOM"/node_allow ]; then
  if [ `grep -x "$ESCSTR" "$ECHO_CUSTOM/node_allow"` ]; then
    LOCKOUT=NO
  fi
fi

if [ "$LOCKOUT" = "YES" ] ; then
  # Incoming party not allowed to connect, inform
  # and disconnect
  TBD_COMMAND message "$ECHO_MYCALL$CURS$DENY_MSG"
  log "Inbound connection from $1 denied (Locked Out)"
  sleep 1
  TBD_COMMAND "play4 -u $1 lockout_remote"
  sleep 10
  TBD_COMMAND disconnect "$1"
  exit 0
fi


if [ ! -f "$LOCAL"/echoirlp ] ; then

   if [ -f "$ACTIVE" ] ; then
      # IRLP or shared conf call in progress.  Inform Echolink user and disconnect.
      if [ -f "$LOCAL"/echo_active ] ; then
         ECHOCONF=`cat "$LOCAL"/echo_call`
         TBD_COMMAND message "$ECHO_MYCALL$CURS"System connected to Echolink shared conference $ECHOCONF. Please try again later.
         $ECHO_SCRIPT/echo_wavgen $LOCAL/echo_call
         $ECHO_SCRIPT/wavprep $AUDIO/busynode $ECHO_AUDIO/echo_id
         log "Busy EchoLink shared conference $ECHOCONF"
      else
         # Strip the REF or STN characters for a better playback experience - VK2XJG
         IRLPNODE=`cat $LOCAL/active | tr -d "stnref"`
         TBD_COMMAND message "$ECHO_MYCALL$CURS""System connected to IRLP node $IRLPNODE.  Please try again later."
         $ECHO_SCRIPT/echo_wavgen "$LOCAL"/active
         $ECHO_SCRIPT/wavprep $AUDIO/busynode $AUDIO/nodenum $ECHO_AUDIO/echo_id
         log "Busy IRLP $IRLPNODE"
      fi
      TBD_COMMAND "play4 -u $1 wavefile"
      sleep 10
      TBD_COMMAND disconnect $1
      exit 0
   else
      if [ -f "$LOCAL"/echoidle ] ; then
         # System is in echo idle state, allow incoming connects 
         echo "${0##*/}: Idle in Echolink state, connection automatically accepted."
         exit 0
      fi
      # Node is idle and accepting calls
      # Need to check if frequency is busy before accepting Echolink call

      if [ -f $LOCAL/cos_active ] ; then
         # System is busy locally, inform connecting EchoLink party
         # and disconnect
         TBD_COMMAND message "$ECHO_MYCALL$CURS"System is busy with local traffic.  Please try again later.
         log "Busy Local"
         sleep 1
         TBD_COMMAND "play4 -u $1 nodeinuse"
         sleep 6
         TBD_COMMAND disconnect "$1"
         exit 0
      fi

      if [ "$REPORTBUSY" = "YES" ] ; then
         if ! $BIN/cosstate ; then
            # System is busy locally, inform connecting EchoLink party
            # and disconnect
            TBD_COMMAND message "$ECHO_MYCALL$CURS"System is busy with local traffic.  Please try again later.
            log "Busy Local"
            sleep 1
            TBD_COMMAND "play4 -u $1 nodeinuse"
            sleep 6
            TBD_COMMAND disconnect $1
            exit 0
         fi
      fi
      # Play welcome file to calling node if exists KF7FLY
      if [ -f "$ECHO_TBD_DATA/welcomeconnect.tbd" ]; then
          echo -n "${0##*/}: Playing welcome file ... "
         TBD_COMMAND "play4 -u $1 welcomeconnect.tbd"  &>/dev/null &
          echo "done"
      fi

      # Set connection files
      # Set IRLP Status as connected to self
      #If the length of the stantionid is 6 (old three digit), it adds a zero
      if [ ${#STATIONID} = "6" ] ; then 
         CONVERTED_STATIONID="$STATIONID"0
      else
         CONVERTED_STATIONID="$STATIONID"
      fi
      echo $CONVERTED_STATIONID > "$LOCAL"/active
      parsehosts "$1"
      if [ "$HOSTSNUM" = "" ] ; then
         log "Could not find $1 in hosts file"
      fi
      echo "$HOSTSNUM" > $LOCAL/echo_active
      echo "$ECHO_TBD_HOST" > $LOCAL/connectedip
      echo "GSM" > $LOCAL/selectcodec
      echo "GSM" > $LOCAL/codec
      echo $1 > $LOCAL/echo_call
      if [ "$ECHO_TBD_LISTEN" = "" ] ; then ECHO_TBD_LISTEN=2074 ; fi
      if [ "$ECHO_TBD_SEND" = "" ] ; then ECHO_TBD_SEND="$ECHO_TBD_LISTEN" ; fi
      if [ "$ECHO_TBD_SEND" != "2074" ] ; then
         echo "$ECHO_TBD_SEND" > "$LOCAL"/udp_port
      fi
      touch "$LOCAL"/echoirlp
   fi

   "$SCRIPT/statupdate" &

   # Set timeout values

   eval `echo $1 | awk '{
        print "FIRSTCHAR="substr($1,1,1)}'`
    
   # If a conference, apply IRLP reflector idle timeout value
   # If not a conference, apply Echolink idle timeout if it is set
   # Otherwise apply IRLP node-node timeout
   if [ "$FIRSTCHAR" = "*" ] ; then  #KF7FLY, modified by VK3JED
     if [ -n "$REFLECT_TIMEOUT_VALUE" ] && [ "$REFLECT_TIMEOUT_VALUE" != "0" ] ; then
       if [ ! -f "$CUSTOM/timeoutvalue.temp" ] ; then # KF7FLY
         cp -f "$CUSTOM/timeoutvalue" "$CUSTOM/timeoutvalue.temp" &>/dev/null
         echo "$REFLECT_TIMEOUT_VALUE" > "$CUSTOM/timeoutvalue"
         killall dtmf &>/dev/null
         "$CUSTOM"/rc.mixer &>/dev/null
         dtmf &>/dev/null
       fi
     else
       rm -f "$LOCAL"/timeout
     fi
   else
     if [ -n "$ECHO_TIMEOUT_VALUE" ]; then
       if [ "$ECHO_TIMEOUT_VALUE" = "0" ]; then
         rm -f "$LOCAL"/timeout
       else
         if [ ! -f "$CUSTOM/timeoutvalue.temp" ] ; then # KF7FLY
           cp -f "$CUSTOM/timeoutvalue" "$CUSTOM/timeoutvalue.temp" &>/dev/null
           echo "$ECHO_TIMEOUT_VALUE" > "$CUSTOM/timeoutvalue"
           killall dtmf &>/dev/null
           "$CUSTOM"/rc.mixer &>/dev/null
           dtmf &>/dev/null
         fi
       fi
     fi 
   fi
    

   if [ "$2" = "notimeout" ] ; then
      rm -f "$LOCAL"/timeout
   fi

   if [ "$CUSTOM_ON" = "YES" ] ; then
      if [ -f $CUSTOM/custom_on ] ; then
         $CUSTOM/custom_on "$1" "$2" "$3" "$4"
      fi
   fi
  
   if [ "$ECHO_SAY_NODE" = "NO" ] ; then
      "$ECHO_SCRIPT"/echo_wavplay echolinkon echo_on
   else
      # callsing or node
     if [ "$ECHO_SAY_NODE" = "YES" ] ; then
        "$ECHO_SCRIPT"/echo_wavgen "$LOCAL"/echo_active
     else
        "$ECHO_SCRIPT"/echo_wavgen "$LOCAL"/echo_call
     fi
     "$ECHO_SCRIPT"/echo_wavplay echolinkon echo_id echo_on
   fi
  
   # Set Environment Variables for Speak Freely
   export SPEAKFREE_CNAME="CALLSIGN"
   export SPEAKFREE_ID="$ECHO_NODE_DESC"::"$ECHO_TBD_PASSWD"
     
   IMIKEFLAG="-t"
   if [ "$3" = "listenonly" ] ; then IMIKEFLAG2="-h -a -s32767,10" ; fi

   $SCRIPT/sfswrapper

   IMIKEFLAG3="$ECHO_TBD_HOST":"$ECHO_TBD_LISTEN"

   if [ "$FORCE_8BIT" = "YES" ] ; then
      "$BIN"/imike -8 "$IMIKEFLAG" $IMIKEFLAG2 $IMIKEFLAG3 &>/dev/null &
   else
      "$BIN"/imike "$IMIKEFLAG" $IMIKEFLAG2 $IMIKEFLAG3 &>/dev/null &
   fi

   # At this point, we should be connected. 
else
   echo "${0##*/}: System already conected."
   # Echolink call is already active, just log connected station and
   # take no further action as IRLP side is already setup
fi
